
var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var inviteuid = 0;
var hasMore = true;
var loadingMore = false;
var page = 1;
Page({
  data: {
    util: util,
    tc: 0,

    isIphone_x: false,
    nav_bgColor: '#ffffff',
    
    token: '',
    uid: 0,
    membercard:'',
    user_jifen: 0,
    sourcelist:[]
  },
  onLoad: function(data){
    
    var that = this;
    var tc = app.get_tc();//简体繁体转换
    console.log("tc======",tc);
    that.setData({
      tc: tc,
    });
    inviteuid = data.inviteuid ? data.inviteuid : '';
    this.getLoginInfo();
    
    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color,
      })
      that.resetNavColor();
    });


    wx.getSystemInfo({
      success: function(res) {
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
          screen_h: res.windowHeight,
        });
      }
    });
    var titleName = tc == 1 ? '積分記錄' : '积分记录';

    wx.setNavigationBarTitle({
      title: titleName,
    })

    //获取关于等信息
    app.getAllAbout('contact', function (res) {
      that.setData({
        contact_phone: res
      })
    });
  },
  onShow:function(){
    var membercard = wx.getStorageSync('membercard');
    this.setData({
      membercard: membercard
    })
    console.log("wx.getStorageSync('membercard')", membercard)
  },


  getJifen: function () {
    var that = this;
    util.api({
      data: {
        "publickey": util.publiccode,
        "ac": "getusersource",
        "uid": that.data.uid
      },
      success: function (responseData) {
        that.setData({
          user_jifen: responseData.data.source
        })
      }
    });
  },

  loadMore: function (e) {
    
    if (hasMore && !loadingMore) {
        loadingMore = true;
        page++;
        console.log("page",page);
        this.getsourcelist(page);
      } else {
        console.log("没有更多了");
      }
    
  },

  //获取积分记录
   getsourcelist: function (cpage) {
    var that = this;
    util.api({
      data: {
        "publickey": util.publiccode,
        "ac": "getsourcelist",
        "uid": that.data.uid,
        "page": cpage,
        "num": 20,
        "tc": that.data.tc
      },
      success: function (responseData) {
        console.log("getsourcelist",responseData);
        var newData = responseData.data;
         if (newData && newData.length > 0) {
          for (var i = 0; i < newData.length; i++) {
              newData[i]['cdate'] = that.toDate(newData[i]['ctime']);
              newData[i]['source'] = JSON.parse(newData[i]['detail']).source;
              // console.log("source",JSON.parse(newData[i]['detail']));
            }
          that.setData({
            sourcelist: that.data.sourcelist.concat(newData),
          })
         }else{
           hasMore = false;
         }
      }
    });
  },

    //时间戳转换时间  
  toDate: function (number) {
    var n = number * 1000;
    var date = new Date(n);
    var cdate = util.formatTime(date,"/",":");
    
    return cdate;

  },

  getLoginInfo: function(){
    var that = this;
    app.getLoginUserInfo(function (res) {
      console.log("mine login:", res);
      that.setData({
        uid: res.uid,
        token: res.token,
        userInfo: res
      });
      that.getJifen();
      that.getsourcelist(1);
    });
  },

  goAtherPage: function(e){
    console.log("goAtherPage",e)
      var url = e.currentTarget.dataset.link;
      wx.navigateTo({ url: url })
  },
  goAtherPageRedirect: function(e){
      var url = e.currentTarget.dataset.link;
      wx.redirectTo({url: url})
  },



  //保存formId
  submitFormId: function (e) {
    app.saveFormId(e.detail.formId);
    var path = e.detail.value.nav_path;
    if (path && path.indexOf('navto:') == 0) {
      path = path.slice(6);
    }
    if (path) {
      wx.reLaunch({
        url: path,
      })
    }
  },



  //页面分享
  onShareAppMessage: function () {
    var invUid = this.data.uid ? this.data.uid : '';

    var path = '/pages/mine/mine?inviteuid=' + invUid;
    console.log("分享给好友：" + path);
     if (util.orgid == "21398") {
      return {
        title: this.data.tc == 1 ? '尚品高質凍肉海產零售批發小程序開通啦，歡迎下單購買！！！':'尚品高质冻肉海产零售批发小程序开通啦，欢迎下单购买！！！',
        path: path
      };
    } else{
      return {
        title: '',
        path: path
      };
    }
    
  },


  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';
    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    });
    this.setData({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },





})